<?php

class CalendarSyncUtil {

    public static function debug($label, $obj = null) {
        echo "<b>{$label}</b>:<br/>";
        if ($obj != null)
            echo "<pre>" . print_r($obj, true) . "</pre>\n\n";
    }

    public static function booleanToStr($bool) {
        return $bool == false ? "false" : "true";
    }

    public static function setEmptyToNull($val) {
        return self::isNullOrEmpty($val) == true ? null : $val;
    }

    public static function isNullOrEmpty($val) {
        return $val == "" || $val == null ? true : false;
    }

    public static function cleanFields($sObj, $fields, $trim = true) {
        $user = new InsightUser();

        if (!$user->hasObj($sObj)) {
            return array();
        } else {
            $accessible_fields = $user->getAccessibleFields($sObj);
            foreach ($fields as $k => $v) {
                if (!Util::in_iarray($k, $accessible_fields))
                    unset($fields[$k]);
                else
                    $fields[$k] = Util::cleanString($v);
            }

            $pruned = self::prune($fields, $trimString = $trim);
            return $pruned;
        }
    }

    // removes null or empty values from an array
    public static function prune($ar, $trimString = true) {
        if (!$ar)
            return $ar;
        $obj = null;
        if (!is_object($ar) && !is_array($ar))
            return $ar;
        if (is_object($ar))
            $obj = new stdClass();
        else if (is_array($ar))
            $obj = array();

        // turn off error reporting here; even for test
        $level = error_reporting();
        error_reporting(0);

        foreach ($ar as $k => $v) {
            if (gettype($v) == "object") {
                $pruned = self::prune($v);

                if (count($pruned)) {
                    if (is_array($ar))
                        $obj[$k] = $pruned;
                    else
                        $obj->$k = $pruned;
                }
            } else if (is_array($v)) {
                if (is_array($obj)) {
                    $obj[$k] = array();
                    foreach ($v as $e => $f) {
                        $obj[$k] = array_merge(
                                $obj[$k], array($e => self::prune($f))
                        );
                    }
                } else {
                    $obj->$k = array();
                    foreach ($v as $e => $f) {
                        $obj->$k = array_merge(
                                $obj->$k, array($e => self::prune($f))
                        );
                    }
                }
            } else if (isset($v) && !(gettype($v) == 'string' && $v == '')) {
                if (gettype($v) == 'string' && $trimString)
                    $v = substr($v, 0, 255); // trim text fields to 255 characters
                if (is_array($ar))
                    $obj[$k] = $v;
                else
                    $obj->$k = $v;
            }
        }

        error_reporting($level);
        return $obj;
    }
    
    public static function getCurrentGMTTime($format = "Y-m-d\TH:i:s.000\Z", $datetime = null) {
        $datetime = $datetime == null ? time() : $datetime;
        $gmdate = gmdate($format, $datetime);
        return $gmdate;
    }
}

?>
